// indexedDB 是一个为了能够在客户端存储可观数量的结构化数据，并且在这些数据上使用索引进行高性能检索的 API。虽然 DOM 存储 对于存储少量数据是非常有用的，但是它对大量结构化数据的存储就显得力不从心了。IndexedDB 则提供了这样的一个解决方案
var dbName = "H5"; //数据库名称
var dbVersion = 2.0; //数据库版本
var tablename = "todo"; //表名

$(function(){
	window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
	if(!window.indexedDB){
	    console.log("你的浏览器不支持IndexedDB");
	}
})
/**
 * [indexdb description]
 * @param  {[type]} action [数据操作动作]
 * @param  {[type]} value  [主键及索引]
 * @param  {[type]} data   [更新数据等等需要传入的值]
 * @return {[type]}        [description]
 */
function indexdb(action,value,data){
	var request = window.indexedDB.open(dbName,dbVersion);
	request.onerror = function(event){
	    console.log("打开indexedDB失败", event);
	}
	request.onupgradeneeded = function(event){
	    console.log("waiting~~");
	    db = event.target.result;
	    var objectStore = db.createObjectStore(tablename,{ keyPath : "rollNo" });
	};
	request.onsuccess = function(event){
	    console.log("成功打开indexedDB 数据库连接~~");
	    db = event.target.result;
	    var transaction = db.transaction(["blockbusters"], "readwrite");
		transaction.oncomplete = function(event) {
		};
		transaction.onerror = function(event) {
			console.error("readwrite data Error");
		};
		var objectStore = transaction.objectStore("blockbusters");  
		if (action == 'find') {
			var request=objectStore.get(value);
			request.onsuccess=function(e){ 
				console.log(action + " data Success");
			}
		}
		if (action == 'select') {
			var request=objectStore.openCursor();
			request.onsuccess=function(e){
				var cursor=e.target.result;
				if(cursor){
					console.log(cursor.key);
					cursor.continue();
				}
			}
		};
		if(action == 'update'){
			var request=objectStore.get(value); 
			request.onsuccess=function(e){ 

				console.log(request);
				console.log(action + " data Success");
			};
		}
		if(action == 'add'){
			result = objectStore.add('blockbusters');
			console.log(action + " data Success");
		}
		if(action == 'del'){
			result = objectStore.delete(value);
			console.log(action + " data Success");
		}
		if('action' == 'clear'){
			result = objectStore.clear(value);
			console.log(action + " data Success");
		};
	};
	request.onupgradeneeded = function(event) {  
		console.log("version Change");
		db = event.target.result;  
		store = db.createObjectStore("blockbusters", {keyPath: "title",autoIncrement: false});    
	}
}
/**
 * [indexdb_del 删除数据库]
 * @param  {[type]} name [数据库名字]
 * @return {[type]}      [description]
 */
function indexdb_del(name){
	indexedDB.deleteDatabase(name);
	console.log(name+'->数据库删除成功^_^');
}